var gotoSearch;
var tips={};
require(['monitor', 'highcharts'], function () {

    $('#monitor').monitor({
        canId: '23',
        callback: function (data) {
            loadData(data);
        },
        clearData: function () {
            loadData({});
        }
    });

    /**
     * 根据具体的值和范围创建圆弧
     * @param val {number} 实际值
     * @param targetId {string} 目标ID
     * @param min {number} 最小值
     * @param max {number} 最大值
     */
    function createCircle(val, targetId, min, max) {
        var $container = $('#' + targetId).empty();
        val = parseInt(val, 10);
        if (isNaN(parseInt(val))) {
            val = min;
        }
        if (val < min) {
            val = min;
        } else if (val > max) {
            val = max;
        }
        var endAngle = Math.PI * (val / (max / 2) - 0.5);
        var renderer = new Highcharts.Renderer(
            $container[0],
            83,
            83
        );
        renderer.arc(41, 41, 41, 39, -0.5 * Math.PI, endAngle).attr({
            fill: '#FF6357',
            stroke: '#FF6357',
            'stroke-width': 0
        }).add();
    }

    //解析
    function loadData(data) {
        //空调开关
        $("#airConditionerSwtc").setView(data.airConditionerSwtc, [
            {value: 0, css: 'status'},
            {value: 1, css: 'status status-active'}
        ], {defaultCss: 'status'});
        //空调运行状态（1 自动  2 ECO  3 通风  4 制热  5 制冷）
        $("#runModel").setView(data.airConditionerRunSt || '-', [
            {value: 0, css: 'status-value', text: '除湿'},
            {value: 1, css: 'status-value', text: '自动'},
            {value: 2, css: 'status-value', text: 'ECO'},
            {value: 3, css: 'status-value', text: '通风'},
            {value: 4, css: 'status-value', text: '制冷'},
            {value: 5, css: 'status-value', text: '制热'},
            {value: 6, css: 'status-value', text: '消毒'},
            {value: 7, css: 'status-value', text: '新风'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //新风状态
        $("#freshSt").setView(data.freshSt != undefined ? data.freshSt : -1, [
            {value: -1, css: 'status-value status-value-gray', text: ''},
            {value: 0, css: 'status-value status-value-gray', text: '关闭'},
            {value: 1, css: 'status-value', text: '开启'}
        ], {unMatchConfig: {css: 'status-value status-value-gray', text: '-'}});
        //风档
        $("#gear").setView(data.gear || 0, [
            {value: 0, css: 'status-value status-value-gray', text: ''},
            {value: 1, css: 'status-value', text: '一档'},
            {value: 2, css: 'status-value', text: '二档'},
            {value: 3, css: 'status-value', text: '三档'},
            {value: 4, css: 'status-value', text: '四档'},
            {value: 5, css: 'status-value', text: '五档'},
            {value: 6, css: 'status-value', text: '六档'},
            {value: 7, css: 'status-value', text: '七档'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //冷凝风机档位
        $("#coolFanGear").setView(data.coolFanGear || 0, [
            {value: 0, css: 'status-value status-value-gray', text: ''},
            {value: 1, css: 'status-value', text: '一档'},
            {value: 2, css: 'status-value', text: '二档'},
            {value: 3, css: 'status-value', text: '三档'},
            {value: 4, css: 'status-value', text: '四档'},
            {value: 5, css: 'status-value', text: '五档'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //发电机电压
        $("#generatorVlt").removeClass();
        if (data.generatorVlt) {
            $("#generatorVlt").html(data.generatorVlt);
            $("#generatorVlt").addClass("status-value");
        }
        else {
            $("#generatorVlt").html("");
            $("#generatorVlt").addClass("status-value status-value-gray");
        }
        //电源电压
        $("#supplyVlt").removeClass();
        if (data.supplyVlt) {
            $("#supplyVlt").html(data.supplyVlt);
            $("#supplyVlt").addClass("status-value");
        }
        else {
            $("#supplyVlt").html("");
            $("#supplyVlt").addClass("status-value status-value-gray");
        }
        //部件状态
        var partsSt = data.partsSt || "--------";
        //管路高压状态（0 断开 1 闭合 2 保留  3不可用）
        $("#partsSt1").setView(partsSt.substr(0, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '断开'},
            {value: 1, css: 'status-value', text: '闭合'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //管路低压状态（0 断开 1 闭合 2 保留  3不可用）
        $("#partsSt2").setView(partsSt.substr(1, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '断开'},
            {value: 1, css: 'status-value', text: '闭合'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //应急开关状态（0 无效 1 有效 2 保留  3不可用）
        $("#partsSt3").setView(partsSt.substr(2, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '无效'},
            {value: 1, css: 'status-value', text: '有效'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //管路中压状态（0 无 1 有 2 错误  3不可用）
        $("#partsSt4").setView(partsSt.substr(3, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '无'},
            {value: 1, css: 'status-value', text: '有'},
            {value: 2, css: 'status-value status-value-warning', text: '错误'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //压缩机状态（0 关机 1 开机 2 保留  3不可用）
        $("#partsSt5").setView(partsSt.substr(4, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '关机'},
            {value: 1, css: 'status-value', text: '开机'},
            {value: 2, css: 'status-value status-value-gray'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //partsSt.substr(5,1)协议保留
        //水泵输出状态（0 无 1 有 2 错误 3 不可用）
        $("#partsSt6").setView(partsSt.substr(6, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '无'},
            {value: 1, css: 'status-value', text: '有'},
            {value: 2, css: 'status-value status-value-gray', text: '错误'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //发电机励磁状态（0 无 1 有 2 错误  3不可用
        $("#partsSt7").setView(partsSt.substr(7, 1), [
            {value: 0, css: 'status-value status-value-gray', text: '无'},
            {value: 1, css: 'status-value', text: '有'},
            {value: 2, css: 'status-value status-value-gray', text: '错误'},
            {value: 3, css: 'status-value', text: '不可用'}
        ], {unMatchConfig: {css: 'status-value status-value-gray'}});
        //消毒状态
        $("#disinfectionSt").setView(data.disinfectionSt != undefined ? data.disinfectionSt : -1, [
            {value: -1, css: 'status-value status-value-gray', text: ''},
            {value: 0, css: 'status-value status-value-gray', text: '关闭'},
            {value: 1, css: 'status-value', text: '开启'}
        ], {unMatchConfig: {css: 'status-value status-value-gray', text: '-'}});
        //化霜状态
        $("#defSt").setView(data.defSt!= undefined&&data.defSt!='' ? data.defSt : -1, [
            {value: -1, css: 'status-value status-value-gray', text: ''},
            {value: 0, css: 'status-value status-value-gray', text: '关闭'},
            {value: 1, css: 'status-value', text: '开启'}
        ], {unMatchConfig: {css: 'status-value status-value-gray', text: '-'}});
        //除湿状态
        $("#dehumidifySt").setView(data.dehumidifySt != undefined&&data.dehumidifySt!='' ? data.dehumidifySt : -1, [
            {value: -1, css: 'status-value status-value-gray', text: ''},
            {value: 0, css: 'status-value status-value-gray', text: '关闭'},
            {value: 1, css: 'status-value', text: '开启'}
        ], {unMatchConfig: {css: 'status-value status-value-gray', text: '-'}});
        //设定温度
        $("#temperatureSet").html((data.temperatureSet || 0) + " <span>℃</span>");
        createCircle(data.temperatureSet || 0, 'temperatureSet-c', 16, 32);
        //车内温度
        $("#temperatureIn").html((data.temperatureIn || 0) + " <span>℃</span>");
        createCircle(data.temperatureIn || 0, 'temperatureIn-c', 16, 32);
        //车外温度
        $("#temperatureOutside").html((data.temperatureOutside || 0) + " <span>℃</span>");
        createCircle(data.temperatureOutside || 0, 'temperatureOutside-c', 16, 32);
        //除霜温度1
        $("#defrostTept1").html((data.defrostTept1 || 0) + " <span>℃</span>");
        createCircle(data.defrostTept1 || 0, 'defrostTept1-c', 16, 32);
        //除霜温度2
        $("#defrostTept2").html((data.defrostTept2 || 0) + " <span>℃</span>");
        createCircle(data.defrostTept2 || 0, 'defrostTept2-c', 16, 32);
        //水温
        $("#waterTept").html((data.waterTept || 0 ) + " <span>℃</span>");
        createCircle(data.waterTept || 0, 'waterTept-c', 0, 100);
        //空调部件故障情况
        // var tips = [
        //     "电源电压DC32V以上",
        //     "发电机电压DC32V以上",
        //     "控制电DC16V 以下",
        //     "发电机电DC16V 以下",
        //     "控制电DC21V 以下",
        //     "发电机电DC21V 以下",
        //     "CAN通讯线路断线",
        //     "CAN通讯线路短路",
        //     "车内温度传感器断线",
        //     "车内温度传感器短路",
        //     "除霜1传感器断线",
        //     "除霜1传感器短路",
        //     "除霜2传感器断线",
        //     "除霜2传感器短路",
        //     "高压异常(断开)",
        //     "低压异常(断开)",
        //     "蒸发风机1或者线路故障",
        //     "蒸发风机2或者线路故障",
        //     "蒸发风机3或者线路故障",
        //     "蒸发风机4或者线路故障",
        //     "蒸发风机5或者线路故障",
        //     "蒸发风机6或者线路故障",
        //     "蒸发风机7或者线路故障",
        //     "蒸发风机8或者线路故障",
        //     "冷凝风机1或者线路故障",
        //     "冷凝风机2或者线路故障",
        //     "冷凝风机3或者线路故障",
        //     "冷凝风机4或者线路故障",
        //     "冷凝风机5或者线路故障",
        //     "冷凝风机6或者线路故障",
        //     "压缩机频繁启动COP(10分钟内启停≥5 次)",
        //     "保留",
        //     "压缩机通讯故障",
        //     "压缩机故障",
        //     "车外温度传感器断线",
        //     "车外温度传感器短路"
        // ];
        var faultDesc = data.faultDesc || "";
        var liList = "";
        var proType=data.proType;
        var faults=tips[proType];
        for (var i = 0; i < faultDesc.length; i++) {
            if (faultDesc.substr(i, 1) == "1") {
                var faultStr = getTipStr(faults, ''+(i + 1));
                if (!$.isEmptyObject(faultStr)&&faultStr != "保留") {
                    liList += "<li class=\"warning\">";
                    liList += faultStr;
                    liList += "</li>";
                }
            }
        }

        if (liList.length > 0) {
            $("#faultDesc").html(liList);
        }
        else {
            $("#faultDesc").html("<li>" + "故障报警-无" + "</li>");
        }
    }

    function getTipStr(faults,key){
        var result="";
        if(!$.isEmptyObject(faults)){
            for(var i=0;i<faults.length;i++){
                var fault = faults[i];
                if(fault.key===key){
                   return fault.value;
                }
            }
        }
        return result;
    }
    
    gotoSearch = function(carId){
    	//切换成线路
		commonFun.showLeftTree(commonFun.carTree);
		//勾选并展开节点
		commonFun.setSelectedNodes(carId);
    };

    //加载故障类型
    var req = $.ajaxExt({
        url: '/airConditioner/getTCommMftCopaAir.htm',
        success: function (result) {
            tips = result || {};
        }
    });
    
});